
	use "${Restricted_Data}/Experian/ExperianMaster.dta", clear
	replace NewUsedIndicator = "N" if New==1
	replace NewUsedIndicator = "U" if New==0
	ta VehicleType
	ren PurchasePrice SellPrice
	gen SaleNumber = 1
	drop if SellPrice == . | SellPrice == 0
	drop month year
	gen month = month(PurchaseDate)
	gen year = year(PurchaseDate)
	gen Experian_ym = ym(year, month)
	format Experian_ym %tm
	drop year month
	duplicates tag VIN PurchaseDate, gen(tag)

	bys VIN PurchaseDate: egen nvalsGroup = nvals(VehicleGroup)
	drop if tag > 0 & nvalsGroup > 1 & regexm(VehicleModel, "Prius") == 1 & VehicleGroup ==  "GROUP 2 - Non-ZEV"
	drop nvalsGroup tag
	duplicates tag VIN PurchaseDate, gen(tag)
	
	replace ReportingPeriod = ReportingPeriod + "01"
	gen ReportingDate = date(ReportingPeriod, "YMD")
	gen DateLag = abs(PurchaseDate - ReportingDate)
	bys VIN PurchaseDate (DateLag): keep if _n == 1
	
	preserve
		use "${Restricted_Data}/Experian/experian_CBG_xwalk.dta", clear
		duplicates tag VIN PurchaseDate, gen(tag)
		bys VIN PurchaseDate (CBG_combined): keep if _n == _N
		tempfile CBG_xwalk_mod
		save `CBG_xwalk_mod', replace
	restore
	merge 1:1 VIN PurchaseDate using `CBG_xwalk_mod'
	tab VehicleType _merge if TitleState == "CA" 
	
	destring CBG_combined, replace
	
	replace OwnerCensusBlockGroup = CBG_combined if mi(OwnerCensusBlockGroup) == 1
	drop if mi(OwnerCensusBlockGroup) == 1
	drop if mi(VehicleType) == 1
	
			ren OwnerCensusBlockGroup cbg
	gen year = year(dofm(Experian_ym))
	keep if VehicleType=="BEV" 
	gen count = 1
	gen NewLeaseDum = LeaseDum*New
	keep Experian_ym year cbg LeaseDum New count NewLeaseDum
	collapse (sum) numBEV = count numNewBEV = New numLeases = LeaseDum numNewLeases = NewLeaseDum, by(Experian_ym year cbg)
	
	gen LeaseFrac = numLeases / numBEV
	gen NewLeaseFrac = numNewLeases / numNewBEV 
	
	fillin Experian_ym cbg
	
	save "$Data_Clean/Experian_lease_extract.dta", replace
	
		
	collapse (sum) numBEV numNewBEV numLeases numNewLeases, by(year cbg)
	
	gen LeaseFrac = numLeases / numBEV
	gen NewLeaseFrac = numNewLeases / numNewBEV 
	
	fillin year cbg
	
	save "$Data_Clean/Experian_lease_extract_year.dta", replace
	